home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / basic / integral.zip / TRAP.BAS < prev    next >
BASIC Source File  |  1994-03-22  |  3KB  |  70 lines

  1. $If 0
  2.  
  3.      ╔═══════════════════════════════════════════════╤═══════════════╗
  4.      ║ Program : Trapazoidal Area                    │ Version : 0.0 ║
  5.      ╟────────────────────────────────┬──────────────┴───────────────╢
  6.      ║ Written by Kurt J. Wolf        │ [X] Released to public       ║
  7.      ║ Copyright 1994 by Kurt J. Wolf │ [ ] Do not release to public ║
  8.      ╟────────────────────────────────┼──────────────────────────────╢
  9.      ║ Compiler : PowerBASIC          │ Compiler Version : 3.0c      ║
  10.      ╚════════════════════════════════╧══════════════════════════════╝
  11.        Home : 39 Brentwood Rd        School : PSU - MA Box 267
  12.               Camp Hill, PA 17011             Mont Alto, PA 17237
  13.               (717) 763-8913                  (717) 749-6430
  14.                                    InterNet : kjw124@psuvm.psu.edu
  15.  
  16.      ───────────────────────────────────────────────────────< Notes >─
  17.      ---This is the PowerBASIC version of a program I have to do
  18.      for my engineering class.  It uses the trapazoid rule to
  19.      evaluate an integral given the two end points and the number
  20.      of sub intervals.
  21.  
  22.  ** Error **
  23.      ---For some odd reason, the change in the x value (DeltaX) is
  24.      0.  This is an incorrect value.  It should be 0.1.
  25.         Lloyd says to use # instead of &.  We will try this.
  26.  ** Fixed **
  27.      ---Changing the & to # fixed the problem.  This will be released
  28.      to public domain.
  29.  
  30.      ---Thank you very much Lloyd!
  31.  
  32.  
  33.                                              n - 1
  34.                        {                -----------]    }
  35.                        |                 \              |
  36.     /\                 |                  \             |
  37.    /  \                |  f(a) + f(b)      \            |
  38.   /----\    =  DeltaX  |  -----------  +    >   f(xi)   |
  39.  /      \              |      2.0          /            |
  40. /        \             |                  /             |
  41.                        |                 /              |
  42.                        {                -----------]    }
  43.                                              i = 1
  44.       Where :   DeltaX = change in x value
  45.                 n = number of subintervals
  46.                 a = lower limit
  47.                 b = upper limit
  48.  
  49.  
  50. $Endif
  51.  
  52. function f#(x#) public
  53.         f# = (x# ^ 2) + 1.0
  54. end function
  55.  
  56. function integrand# (lower#, upper#, n#) public
  57.         deltax# = (upper#-lower#) / n#
  58.         for i# = 1 to n# - 1
  59.                 x# = x# + deltax#
  60.                 sum# = f#(x#) + sum#
  61.         next i#
  62.         sum# = ((f#(lower#) + f#(upper#)) / 2.0) + sum#
  63.         sum# = sum# * deltax#
  64.         integrand# = sum#
  65. end function
  66.  
  67. print str$(integrand#(0, 1, 10))
  68. print str$(integrand#(0, 1, 20))
  69. print str$(integrand#(0, 1, 100))
  70.